"use strict";

let express = require('express');
let router = express.Router();
let shell = require('shelljs');
let shellConf = require("../config/shell.json");

/* GET home page. */
router.get('/', function (req, res, next) {
    res.render('index', {title: 'Express'});
});

router.post("/webhook", function (req, res) {
    let data = req.body;

    if (data.password == "auwaiwing666") {
        if (shellConf[data.project.name]) {
            let command = shellConf[data.project.name].command;
            let nowBranch = data.ref.replace(/refs\/heads\//g, "");
            command = command.replace(/:now_branch/g, nowBranch);

            shell.exec(command);

            res.send({success: true, message: "部署成功"});
        } else {
            res.send({success: false, message: "该项目不存在"});
        }
    } else {
        res.send({success: false, message: "部署失败"});
    }

});


module.exports = router;
